//
//  FAlertView.m
//  JoinUser
//
//  Created by xujingjing on 16/6/15.
//  Copyright © 2016年 xujingjing. All rights reserved.
//

#import "FAlertView.h"

#define AWidth   [UIScreen mainScreen].bounds.size.width
#define AHeight    [UIScreen mainScreen].bounds.size.height

@interface FAlertView ()<UITextFieldDelegate>
{
    UIImageView * imageview;
    
    NSMutableArray * dataSource;
    UITextField * topTX;
}

@property (nonatomic,strong) UILabel * msgLabel;
@property (nonatomic,strong) UIView  * mainView;

@end

@implementation FAlertView
- (instancetype)initWithFrame:(CGRect)frame
                          msg:(NSString *)msg
                        delay:(NSTimeInterval)delay
{
    self = [super initWithFrame:frame];
    if (self) {
        self.msgLabel = [[UILabel alloc] init];
        self.msgLabel.bounds = CGRectMake(0, 0, 150, 60);
        self.msgLabel.center = CGPointMake(self.center.x, self.center.y - 20);
        self.msgLabel.backgroundColor = [UIColor blackColor];
        self.msgLabel.text = msg;
        self.msgLabel.numberOfLines = 0;
        self.msgLabel.textColor = [UIColor whiteColor];
        self.msgLabel.textAlignment = NSTextAlignmentCenter;
        self.msgLabel.layer.cornerRadius = 6;
        self.msgLabel.layer.masksToBounds = YES;
        [self addSubview:self.msgLabel];
        
        [self performSelector:@selector(cancelAlert) withObject:nil afterDelay:delay];
    }
    return self;
}


- (instancetype)initWithFrame:(CGRect)frame
                    showTitle:(NSString *)title
                      message:(NSString *)message
                     delegate:(id)delegate
               canbuttontitle:(NSString *)cantitle
                  otherbutton:(NSString *)otherbuttontitle
{
    self = [super initWithFrame:frame];
    if (self) {
        UIView * backview = [[UIView alloc]initWithFrame:CGRectMake(0, 0,AWidth - 40 , (AWidth - 40)/4*3)];
        backview.center =CGPointMake(self.center.x, self.center.y - 20);

        backview.backgroundColor = [UIColor whiteColor];
        backview.layer.cornerRadius = 8;
        backview.layer.masksToBounds = YES;
        [self addSubview:backview];
        
        UILabel * titlelable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, backview.frame.size.width, 50)];
        titlelable.text = title;
        titlelable.textColor = [UIColor blackColor];
        titlelable.font = [UIFont systemFontOfSize:16];
        titlelable.textAlignment = NSTextAlignmentCenter;
        [backview addSubview:titlelable];
        CALayer * line = [[CALayer alloc]init];
        line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.7].CGColor;
        line.frame = CGRectMake(0, 50, backview.frame.size.width, 1);
        [backview.layer addSublayer:line];
        
        UILabel * messagelable = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, backview.frame.size.width-20, backview.frame.size.height - 45 -80)];
        messagelable.textColor = [UIColor grayColor];
        messagelable.textAlignment = NSTextAlignmentCenter;
        messagelable.numberOfLines = 0;
        messagelable.text = message;
        messagelable.adjustsFontSizeToFitWidth = YES;
        messagelable.font = [UIFont systemFontOfSize:14];
//        messagelable.layer.borderWidth = 1;
        [backview addSubview:messagelable];
        
        for (int i = 0; i < 2; i ++) {
            UIButton * chosebutton = [[UIButton alloc]initWithFrame:CGRectMake(((backview.frame.size.width-20)/2 +5)*i + 10, CGRectGetMaxY(messagelable.frame)+5, (backview.frame.size.width - 30)/2, 40)];
            chosebutton.layer.borderWidth = 1;
            chosebutton.layer.cornerRadius = 4;
            chosebutton.layer.masksToBounds = YES;
            [chosebutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            if (i ==  0) {
                [chosebutton setTitle:cantitle forState:UIControlStateNormal];
                chosebutton.layer.borderColor = [UIColor colorWithWhite:0.9 alpha:0.5].CGColor;
            }else
            {
                [chosebutton setTitle:otherbuttontitle forState:UIControlStateNormal];
                chosebutton.backgroundColor = [UIColor blueColor];
                chosebutton.layer.borderColor = [UIColor blueColor].CGColor;
            }
            chosebutton.tag = 2000+i;
            [chosebutton addTarget:self action:@selector(choseBtn:) forControlEvents:UIControlEventTouchUpInside];
            [backview addSubview:chosebutton];
            self.delegate = delegate;
        }
        
        
    }
   
    return self;

}


-(instancetype)initWithFrame:(CGRect)frame
                   showTitle:(NSString *)title
                     message:(NSString *)message
                       money:(float)money
                    delegate:(id)delegate
{
    self = [super initWithFrame:frame];
    if (self) {
        UIView * backview = [[UIView alloc]initWithFrame:CGRectMake(0, 0,AWidth - 40 , (AWidth - 40)/4*3)];
        backview.center =CGPointMake(self.center.x, self.center.y - 100);
        
        backview.backgroundColor = [UIColor whiteColor];
        backview.layer.cornerRadius = 8;
        backview.layer.masksToBounds = YES;
        [self addSubview:backview];
        
        UILabel * titlelable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, backview.frame.size.width, 50)];
        titlelable.text = title;
        titlelable.textColor = [UIColor blackColor];
        titlelable.font = [UIFont systemFontOfSize:16];
        titlelable.textAlignment = NSTextAlignmentCenter;
        [backview addSubview:titlelable];
        UIButton * rightbutton = [[UIButton alloc]initWithFrame:CGRectMake(backview.frame.size.width-30, 5, 20, 20)];
        [rightbutton setTitle:@"X" forState:UIControlStateNormal];
        [rightbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [rightbutton addTarget:self action:@selector(cancelAlert) forControlEvents:UIControlEventTouchUpInside];
        [backview addSubview:rightbutton];
        CALayer * line = [[CALayer alloc]init];
        line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.7].CGColor;
        line.frame = CGRectMake(0, 50, backview.frame.size.width, 1);
        [backview.layer addSublayer:line];
        
        UILabel * messagelable = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, backview.frame.size.width-20, 30)];
        messagelable.textColor = [UIColor grayColor];
        messagelable.textAlignment = NSTextAlignmentCenter;
        messagelable.numberOfLines = 0;
        messagelable.text = @"申请提现";
        messagelable.adjustsFontSizeToFitWidth = YES;
        messagelable.font = [UIFont systemFontOfSize:14];
        [backview addSubview:messagelable];
        
        UILabel * moneylable = [[UILabel alloc]initWithFrame:CGRectMake(10, 90, backview.frame.size.width-20, 35)];
        moneylable.textColor = [UIColor grayColor];
        moneylable.textAlignment = NSTextAlignmentCenter;
        moneylable.numberOfLines = 0;
        moneylable.text =[NSString stringWithFormat:@"￥%0.2lf",money];
        messagelable.font = [UIFont systemFontOfSize:16];
        [backview addSubview:moneylable];
        
        topTX = [[UITextField alloc] initWithFrame:CGRectMake(10, 140, backview.frame.size.width-20, 20)];
         topTX.hidden = YES;
         topTX.keyboardType = UIKeyboardTypeNumberPad;
         [topTX addTarget:self action:@selector(txchange:) forControlEvents:UIControlEventEditingChanged];
         [backview addSubview:topTX];
         //进入界面，topTX成为第一响应
         [topTX becomeFirstResponder];
        dataSource = [NSMutableArray array];
         for (int i = 0; i < 6; i++)
         {
             UITextField *pwdLabel = [[UITextField alloc] initWithFrame:CGRectMake(10+i*(backview.frame.size.width-20)/6 , 140, (backview.frame.size.width-20)/6 , (backview.frame.size.width-20)/6)];
             pwdLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
             pwdLabel.backgroundColor = [UIColor whiteColor];
             pwdLabel.enabled = NO;
             pwdLabel.textAlignment = NSTextAlignmentCenter;//居中
             pwdLabel.secureTextEntry = YES;//设置密码模式
             pwdLabel.layer.borderWidth = 0.5;
             [backview addSubview:pwdLabel];

             [dataSource addObject:pwdLabel];
         }


        
    }
    
    return self;
    

}





#pragma mark 文本框内容改变
- (void)txchange:(UITextField *)tx
{
    NSString *password = tx.text;
    NSLog(@"%@",tx.text);
        if (password.length == dataSource.count)
        {
            [tx resignFirstResponder];//隐藏键盘
        }
    for (int i = 0; i < dataSource.count; i++)
    {
        UITextField *pwdtx = [dataSource objectAtIndex:i];
        if (i < password.length)
        {
            NSString *pwd = [password substringWithRange:NSMakeRange(i, 1)];
            pwdtx.text = pwd;
        }else{
            pwdtx.text = @"";
        }
        
    }
    
        if (password.length == 6)
        {
            [self.delegate backkey:password];
            [self cancelAlert];
            
        }
}



-(void)choseBtn:(UIButton *)sender{
 
    if (_delegate && [_delegate respondsToSelector:@selector(FAlertChoseBtnwithNumber:)]) {
        // *** 调用代理对象方法
      [self.delegate FAlertChoseBtnwithNumber:(sender.tag - 2000)];
    }
    
    
    [self cancelAlert];
}

- (void)confirmBtnClick {
    //*** 判断代理对象是否存在并且响应协议方法
    if (_delegate && [_delegate respondsToSelector:@selector(alertClickConfirm)]) {
        // *** 调用代理对象方法
        [_delegate alertClickConfirm];
    }
    //取消掉弹窗
    [self cancelAlert];
}

- (void)cancelBtnClick {
    [self cancelAlert];
}


- (void)cancelAlert {
    //将视图从父视图移除
    [UIView animateWithDuration:0.5 animations:^{
        
        self.alpha = 0.1;
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];

    }];
    
    
}

+ (void)showMsg:(NSString *)msg
    cancelDelay:(NSTimeInterval)delay {
    //*** 获取应用程序window
    UIWindow * keyWindow = [[[UIApplication sharedApplication] delegate] window];
    FAlertView * alert = [[FAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds] msg:msg delay:delay];
   // [alert setBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.4]];
    
    [UIView animateWithDuration:0.1 animations:^{
        alert.alpha = 1;

    } completion:^(BOOL finished) {
        
        [keyWindow addSubview:alert];

        
    }];
}


+(void)showTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate canbuttontitle:(NSString *)cantitle otherbutton:(NSString *)otherbuttontitle
{
    UIWindow * keyWindow = [[[UIApplication sharedApplication] delegate] window];
    FAlertView * alert = [[FAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds showTitle:title message:message delegate:delegate canbuttontitle:cantitle otherbutton:otherbuttontitle];
    [alert setBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.4]];
    [keyWindow addSubview:alert];
}


+(void)showTitle:(NSString *)title message:(NSString *)message money:(float)money delegate:(id)delegate
{
    UIWindow * keyWindow = [[[UIApplication sharedApplication] delegate] window];
    FAlertView * alert = [[FAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds showTitle:title message:message money:money delegate:delegate];
    [alert setBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.4]];
    [keyWindow addSubview:alert];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
